package cn.gyouzhe.vo;

import lombok.Getter;
import lombok.ToString;

import java.util.ArrayList;
import java.util.List;

/**
 * 分页结果集
 *
 * @author wangchuan
 * @datetime 2017/11/24 23:27
 */
@Getter
@ToString
public class PageResult<T> extends Result {
    private int total;
    private int totalPage;
    private int currentPage;
    private int start;
    private int limit;
    private boolean hasPre;
    private boolean hasNext;
    private List<T> data;

    private PageResult(int currentPage, int limit, int total, List<T> data) {
        this.total = total;
        this.limit = limit;
        this.currentPage = currentPage;
        this.totalPage = total % limit == 0 ? total / limit : total / limit + 1;
        this.start = (currentPage - 1) * limit;
        this.hasPre = currentPage > 1;
        this.hasNext = currentPage < this.totalPage;
        this.data = data;
    }


    private PageResult(int currentPage, int limit, int total) {
        this.total = total;
        this.limit = limit;
        this.currentPage = currentPage;
        this.totalPage = total % limit == 0 ? total / limit : total / limit + 1;
        this.start = (currentPage - 1) * limit;
        this.hasPre = currentPage > 1;
        this.hasNext = currentPage < this.totalPage;
    }

    public static <T> PageResult<T> of(int currentPage, int limit, int total) {
        return new PageResult<>(currentPage, limit, total);
    }

    public static <T> PageResult<T> of(int currentPage, int limit, int total, List<T> data) {
        return new PageResult<>(currentPage, limit, total, data);
    }

    /**
     * 添加数据
     * @param t
     */
    public void appendData(T t) {
        if(null == data)
            data = new ArrayList<>();
        data.add(t);
    }
}
